Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
React JS Detect link from text this is my [web](https://www.google.com/)

Do anyone have suggestions to detect link from text Currently in react I am just checking the regex for link using below code:

 urlify(text) {
  var urlRegex = (new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?"));
   return text.replace(urlRegex, function (url) {
   return '<a href="' + url + '" target="_blank">' + url + '</a>';
 });}

  render() {
   let description="this is my [web](http://stackoverflow.com), this is [Google](https://www.google.com/)"
    return (
    <p dangerouslySetInnerHTML={{__html: this.urlify(description)}}></p>
   );}

The output for above code displayed as shown here

But I just wanna display text as This is my web

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you wanted to continue using dangerouslySetInnerHTML you could use this match/replace to create an anchor...

const text = 'this is my [web](https://www.google.com/)'

const regex = /(.+)\[(.+)\]\((.+)\)/;

const anchor = text.replace(regex, (match, a, b, c) => {
  const text = `${a[0].toUpperCase()}${a.substring(1)}${b}`;
  return `<a href="${c}">${text}</a>`;
});

console.log(anchor);

...or you could create a bespoke component that maps the array output from the match to some JSX that creates an anchor.

function MarkdownAnchor({ markdown }) {

  const regex = /(.+)\[(.+)\]\((.+)\)/;
  const match = markdown.match(regex);

  function formatText(str) {
    return `${str[0].toUpperCase()}${str.substring(1)}`
  }

  return (
    <a href={match[3]}>
      {formatText(`${match[1]}${match[2]}`)}
    </a>
  );

}

const markdown = 'this is my [web](https://www.google.com/)';

ReactDOM.render(
  <MarkdownAnchor markdown={markdown} />,
  document.getElementById('react')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda